Existing Statistics
Gem5 comes with a variety of pre-configured and useful statistics. In order to get a good handle on what one of those statistic implementations looks like, we should first consider the statistics that are already implemented.
Registering Statistics
Let's take a look at the src/cpu/o3/iew.hh
file (you'll need to play with this one some more in your assignment) around line 418:
struct IEWStats : public statistics::Group
{
IEWStats(CPU *cpu);
/** Stat for total number of idle cycles. */
statistics::Scalar idleCycles;
...
This is the first step to registering a statistic in gem5. this allows us to declare the statistic type and the eventual display name. Gem5 statistics come with the following possible types:
- Scalar: A single value, e.g. cycles
- Vector: A vector of values, e.g. tags by block age
- Distribution: A distribution of values, e.g. snoop fanout
- Formula: An arbitrary mathematical formula of other stats
You can find the actual declarations of these types in the gem5 source tree.
You will be dealing with the scalar statistics in your assignments.
Instantiating Statistics
Now we need to switch over to the src/cpu/o3/iew.cc
file. You can find another odd looking function at about line 144:
IEW::IEWStats::IEWStats(CPU *cpu)
: statistics::Group(cpu, "iew"),
ADD_STAT(idleCycles, statistics::units::Cycle::get(),
"Number of cycles IEW is idle"),
ADD_STAT(squashCycles, statistics::units::Cycle::get(),
"Number of cycles IEW is squashing"),
...
This is where we actually instantiate our statistics and make them into objects that we can add onto. You will note that we need to use the name of our statistic, the type, and the comment underneath defines the comment that appears after the statistic in the stats.txt
file.
The most commonly used type is the statistics::units::Count::get()
type, and this is a simple statistic that you can increment as required, similarly to what you need to do your branch prediction statistics aggregation.
Using Statistics
If we scroll a bit further into the src/cpu/o3/iew.cc
file, we can see an example use of the instsToCommit
statistic at line 1358:
iewStats.inststoCommit[tid]++;
Now, this is a bit of a special case, seeing as the instsToCommit stat is for each individual thread, but the intuition remains the same, the stats essentially form global variables that you can use to bookkeep any type of information you want.